package com.fan.play.media.capture.video

import android.content.Context
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CameraManager
import com.fan.play.media.common.MediaDefine
import com.fan.play.media.engine.CaptureDeviceCamera
import com.fan.play.utils.AppUtil

/**
 * author: x-fan
 * date: 2024/1/18
 * desc: 相机信息
 */
class Camera2Info {
    companion object {
        @JvmStatic
        fun getCameraCount(): Int {
            val cameraManager = AppUtil.getService<CameraManager>(Context.CAMERA_SERVICE)
            return cameraManager.cameraIdList.size
        }

        @JvmStatic
        fun getCameraInfo(cameraID: Int): CaptureDeviceCamera {
            val cameraManager = AppUtil.getService<CameraManager>(Context.CAMERA_SERVICE)
            val characteristic = cameraManager.getCameraCharacteristics(cameraID.toString())
            val captureDevice = CaptureDeviceCamera()
            captureDevice.devID = cameraID
            captureDevice.devType = MediaDefine.DeviceType_Camera
            captureDevice.devState = MediaDefine.DeviceState_None

            val fpsList =
                characteristic.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES)
            var minFPS = MediaDefine.MediaFrameRate
            var maxFPS = MediaDefine.MediaFrameRate
            fpsList?.forEach {
                minFPS = minFPS.coerceAtMost(it.lower)
                maxFPS = maxFPS.coerceAtLeast(it.upper)
            }

            captureDevice.frameRate = if (minFPS > MediaDefine.MediaFrameRate) {
                minFPS
            } else if (maxFPS < MediaDefine.MediaFrameRate) {
                maxFPS
            } else {
                MediaDefine.MediaFrameRate
            }

            return captureDevice
        }
    }
}